package com.fessc.develop.utils;

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

@Slf4j
public class FileToBase64 {

    public static String getFileToBase64(String filePath) {
        String base64EncodedString = "";
        try {
            // 读取文件内容
            byte[] fileContent = Files.readAllBytes(Paths.get(filePath));
            // 使用Base64编码
            base64EncodedString = Base64.getEncoder().encodeToString(fileContent);
        } catch (Exception e) {
            log.info("读取文件异常：" + e.getMessage());
        }
        return base64EncodedString;
    }

    public static Boolean getBase64ToFile(String filePath, String base64EncodedString) {
        FileOutputStream out = null;
        boolean retValue = false;
        try {
            byte[] buffer;
            File file = new File(filePath);
            BASE64Decoder base64Encoded = new BASE64Decoder();
            buffer = base64Encoded.decodeBuffer(base64EncodedString);
            out = new FileOutputStream(file);
            out.write(buffer);
            retValue = true;
        } catch (Exception e) {
            log.info("写入文件异常：" + e.getMessage());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception ex) {
                log.info("关闭文件流异常：" + ex);
            }
        }
        return retValue;
    }

    public static String readTextFileToString(String filePathName, String encoding) {
        String strTemp;
        StringBuffer result = new StringBuffer();
        BufferedReader freader = null;
        String lineSeparator = System.getProperties().getProperty("line.separator");
        try {
            freader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(filePathName)), encoding));
            while ((strTemp = freader.readLine()) != null) {
                result.append(strTemp);
                result.append(lineSeparator);
            }
        } catch (Exception e) {
            log.info("读取文件失败：" + e.getMessage());
        } finally {
            try {
                if (freader != null) {
                    freader.close();
                }
            } catch (Exception ex) {
                log.info("关闭文件流失败：" + ex.getMessage());
            }
        }
        if (result.length() > lineSeparator.length()) {
            return result.substring(0, result.length() - lineSeparator.length());  //去掉最后一个换行符
        } else {
            return result.toString();
        }
    }

    public static void byteToFile(String filePathName, byte[] bytes) {
        BufferedOutputStream outputStream = null;
        try {
            // 写入字节到文件
            outputStream = new BufferedOutputStream(Files.newOutputStream(Paths.get(filePathName)));
            outputStream.write(bytes);
        } catch (IOException e) {
            log.info("写入文件失败：" + e.getMessage());
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception ex) {
                log.info("关闭文件流异常：" + ex.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        String base64EncodedString = "";
        try {
            // 读取文件内容
            byte[] fileContent = Files.readAllBytes(Paths.get("D:\\Code\\Data\\1.txt"));
            // 使用Base64编码
            base64EncodedString = Base64.getEncoder().encodeToString(BusiUtil.zip(fileContent));
            log.info("base64===" + base64EncodedString);
        } catch (Exception e) {
            log.info("读取文件异常：" + e.getMessage());
        }
    }

}
